package com.loyal.mwm.ext

import android.text.TextUtils
import com.base.kit.GsonUtil
import com.loyal.mwm.beans.ResultBean

fun String?.toBean(): com.loyal.mwm.beans.ResultBean<*>? {
    if (TextUtils.isEmpty(this))
        return null
    return try {
        GsonUtil.json2Bean(this, com.loyal.mwm.beans.ResultBean::class.java)
    } catch (e: Exception) {
        null
    }
}

fun <T> String?.toObj(objClass: Class<T>): com.loyal.mwm.beans.ResultBean<T?>? {
    if (TextUtils.isEmpty(this))
        return null
    return try {
        GsonUtil.json2Object(this, com.loyal.mwm.beans.ResultBean::class.java, objClass) as com.loyal.mwm.beans.ResultBean<T?>?
    } catch (e: Exception) {
        null
    }
}

fun <T> String?.toArray(objClass: Class<T>): com.loyal.mwm.beans.ResultBean<List<T>>? {
    return try {
        GsonUtil.json2Array(this, com.loyal.mwm.beans.ResultBean::class.java, objClass) as com.loyal.mwm.beans.ResultBean<List<T>>?
    } catch (e: Exception) {
        null
    }
}

fun <T> String?.toBean(objClass: Class<T>): T? {
    return GsonUtil.json2Bean(this, objClass)
}

fun <T> String?.asList(objClass: Class<T>): List<T> {
    return GsonUtil.json2List(this, objClass)
}
